package com.ruoyi.web.invest.asset.asset.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.web.invest.asset.asset.domain.Asset;
import com.ruoyi.web.invest.asset.asset.service.IAssetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 资产Controller
 *
 * @author feng_zi_xiang
 * @date 2021-08-27
 */
@RestController
@RequestMapping("/invest.asset/asset")
public class AssetController extends BaseController {
    @Autowired
    private IAssetService assetService;

    /**
     * 查询资产列表
     */
    @GetMapping("/list")
    public TableDataInfo list(Asset asset) {
        startPage();
        List<Asset> list = assetService.selectAssetList(asset);
        return getDataTable(list);
    }

    /**
     * 查询资产列表
     */
    @GetMapping("/listForRepo")
    public TableDataInfo listForRepo(Asset asset) {
        startPage();
        List<Asset> list = assetService.selectAssetListForRepo(asset);
        return getDataTable(list);
    }

    /**
     * 导出资产列表
     */
    @Log(title = "资产", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Asset asset) {
        List<Asset> list = assetService.selectAssetList(asset);
        ExcelUtil<Asset> util = new ExcelUtil<Asset>(Asset.class);
        return util.exportExcel(list, "资产数据");
    }

    /**
     * 获取资产详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(assetService.selectAssetById(id));
    }

    /**
     * 新增资产
     */
    @Log(title = "资产", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Asset asset) {
        return toAjax(assetService.insertAsset(asset));
    }

    /**
         * 卖出股票资产
         */
        @Log(title = "资产", businessType = BusinessType.UPDATE)
        @GetMapping(value = "/sell")
        public AjaxResult edit(@RequestParam(value = "assetId") Long assetId,
                @RequestParam(value = "price") String price,
        @RequestParam(value = "number") int number) {
            return toAjax(assetService.sell(assetId, price, number));
    }

    /**
     * 卖出回购股票
     */
    @Log(title = "资产", businessType = BusinessType.UPDATE)
    @GetMapping(value = "/sellForRepo")
    public AjaxResult update(@RequestParam(value = "assetId") Long assetId,
                           @RequestParam(value = "price") String price,
                           @RequestParam(value = "number") int number) {
        return toAjax(assetService.sellForRepo(assetId, price, number));
    }

    /**
     * 删除资产
     */
    @Log(title = "资产", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(assetService.deleteAssetByIds(ids));
    }
}
